package com.chenshu;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-13
 * Time：22:10
 */

//@Controller
public class BeanLifeController implements BeanNameAware{

    //@Autowired
    private MyBean bean;

    @Autowired
    public void setMyBean(MyBean myBean) {
        this.bean = myBean;
    }

    public BeanLifeController() {
        System.out.println("BeanLifeController的构造方法");
    }
    @Override
    public void setBeanName(String s) {
        System.out.println("执行了Bean Name 的通知: " + s);
    }

    //方法名可以随意定义，xml中init-method指定的方法
    public void initMethod() {
        System.out.println("执行了 init method 方法");
        bean.doBean();
    }

    @PostConstruct
    public void myPostConstruct() {
        System.out.println("执行了 PostConstruct 方法");
        bean.doBean();
    }

    /**
     * 销毁前执行方法
     */
    @PreDestroy
    public void myPreDestroy() {
        System.out.println("执行了 PreDestroy 方法");
    }

    public void use() {
        System.out.println("执行了use方法");
        bean.doBean();
    }

}
